<template>
  <div class="good-order">
    <van-nav-bar title="商品订单" left-arrow @click-left="this.$router.replace('/my')" />
    <div class="good-content">
      <van-tabs v-model:active="active" @click-tab="onClickTab">
        <van-tab v-for="item in lablist" :key="item.id" :title="item.name">
          <div class="good-card" v-for="item in cardlist" :key="item.id">
            <div class="head">
              <div class="left">
                <img
                  :src="item.shop.pic ? 'http://192.168.101.65:9000' + item.shop.pic : pic"
                  alt=""
                />
              </div>
              <div class="main">
                <div class="name">{{ item.shop.name }}</div>
                <span class="remark">{{ item.shop.remark }}</span>
              </div>
              <p class="right">
                <span>￥{{ item.shop.price }}</span>
                <span class="count">x 1</span>
              </p>
            </div>
            <div class="footer">
              <span>商品积分 ￥{{ item.shop.price }}</span>
            </div>
          </div>
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>

<script>
import { showToast } from 'vant'
import { ShopOrder } from '../../api/user.js'
export default {
  data() {
    return {
      pic: require('../../assets/image/default.png'),
      // tab的激活项
      active: Number(window.sessionStorage.getItem('shopActive')),
      // 标签
      lablist: [
        { id: 1, name: '待发货' },
        { id: 2, name: '待收货' },
        { id: 3, name: '历史订单' }
      ],
      // 商品订单数据
      goodlist: [],
      // 渲染的商品卡片
      cardlist: []
    }
  },
  created() {
    this.getshoplist()
  },
  methods: {
    async getshoplist() {
      const { data: res } = await ShopOrder()
      if (res.code === 1) {
        this.goodlist = res.data
        if (this.$route.params.active !== '2') {
          this.cardlist = this.goodlist.filter(
            (item) => item.status === String(this.$route.params.active)
          )
        } else {
          this.cardlist = this.goodlist
        }
      } else {
        showToast('获取数据失败！')
      }
    },
    onClickTab({ name }) {
      this.active = name
    }
  },
  watch: {
    active: {
      immediate: true,
      async handler(newVal) {
        switch (newVal) {
          case 0:
            this.cardlist = this.goodlist.filter((item) => item.status === '0')
            break
          case 1:
            this.cardlist = this.goodlist.filter((item) => item.status === '1')
            break
          case 2:
            this.cardlist = this.goodlist
            break
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.good-order {
  width: 100%;
  height: 100vh;
  box-sizing: border-box;

  .good-content {
    // 商品卡片
    .good-card {
      width: 95%;
      padding: 7px;
      margin: 10px;
      box-sizing: border-box;
      background-color: #fff;
      border-radius: 10px;

      .head {
        display: flex;

        .left {
          width: 34%;
          height: 100px;
          img {
            width: 100%;
            height: 100%;
          }
        }
        .main {
          width: 51%;
          height: 60px;
          margin-left: 6px;

          .name {
            width: 100%;
            margin: 10px 0;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;

            font-size: 15px;
          }

          .remark {
            padding: 3px 7px;
            border-radius: 6px;
            border: 1px solid #87ceeb;
            color: #87ceeb;
            font-size: 13px;
          }
        }
        .right {
          position: relative;
          display: flex;
          flex-direction: column;
          align-items: end;
          justify-content: space-between;
          width: 10%;
          margin-top: 10px;
          background-color: #fff;

          .count {
            margin-bottom: 7px;
          }
        }
      }
      .footer {
        padding-right: 5px;
        text-align: right;

        span {
          color: #ec7411;
          border: 1px solid #ec7411;
          padding: 2px 5px;
          border-radius: 6px;
          font-size: 16px;
        }
      }
    }
  }
}
</style>
